package cn.edu.scau.cmi.ema.dao;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.domain.Fillertype;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

@Repository("FillertypeDAO")
@Transactional
public class FillertypeDAOImpl extends AbstractJpaDao<Fillertype> implements FillertypeDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Fillertype.class }));

	@PersistenceContext()
	private EntityManager entityManager;

	public FillertypeDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Fillertype findFillertypeById(Integer id) throws DataAccessException {

		return findFillertypeById(id, -1, -1);
	}

	@Transactional
	public Fillertype findFillertypeById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFillertypeById", startResult, maxRows, id);
			return (Fillertype) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Fillertype> findFillertypeByName(String name) throws DataAccessException {

		return findFillertypeByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Fillertype> findFillertypeByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findFillertypeByName", startResult, maxRows, name);
		return new LinkedHashSet<Fillertype>(query.getResultList());
	}

	@Transactional
	public Set<Fillertype> findFillertypeByNameContaining(String name) throws DataAccessException {

		return findFillertypeByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Fillertype> findFillertypeByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFillertypeByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Fillertype>(query.getResultList());
	}

	@Transactional
	public Set<Fillertype> findFillertypeByParent(Integer parent) throws DataAccessException {

		return findFillertypeByParent(parent, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Fillertype> findFillertypeByParent(Integer parent, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFillertypeByParent", startResult, maxRows, parent);
		return new LinkedHashSet<Fillertype>(query.getResultList());
	}

	@Transactional
	public Set<Fillertype> findFillertypeByDescription(String description) throws DataAccessException {

		return findFillertypeByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Fillertype> findFillertypeByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFillertypeByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Fillertype>(query.getResultList());
	}

	@Transactional
	public Set<Fillertype> findFillertypeByDescriptionContaining(String description) throws DataAccessException {

		return findFillertypeByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Fillertype> findFillertypeByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFillertypeByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Fillertype>(query.getResultList());
	}

	@Transactional
	public Fillertype findFillertypeByPrimaryKey(Integer id) throws DataAccessException {
		return findFillertypeByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Fillertype findFillertypeByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFillertypeByPrimaryKey", startResult, maxRows, id);
			return (Fillertype) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Fillertype> findFillertypesByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFillertypesByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Fillertype> findAllFillertypes() throws DataAccessException {

		return findAllFillertypes(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Fillertype> findAllFillertypes(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllFillertypes", startResult, maxRows);
		return new LinkedHashSet<Fillertype>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Fillertype> findFillertypeByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Fillertype>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Fillertype> findFillertypesByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Fillertype.class);
		Session session = (Session) entityManager.getDelegate();
		Set<Fillertype> fillertypes = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return fillertypes;
	}

	@Transactional
	public Fillertype findFillertypeByPhysicalPrimaryKey(String name, Integer parent) throws DataAccessException {
		return findFillertypeByPhysicalPrimaryKey(name, parent, -1, -1);
	}

	@Transactional
	public Fillertype findFillertypeByPhysicalPrimaryKey(String name, Integer parent, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findFillertypeByPhyscialPrimaryKey", startResult, maxRows, name, parent);
			return (Fillertype) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Fillertype entity) {
		return true;
	}
}
